﻿using KPIS.GERP.Models.Class;
using System;
using System.Linq;
using System.Web.Mvc;
using Resources;
using KPIS.GERP.Models.ICM;

namespace KPIS.GERP.WEB.GIMS.Controllers
{
    // [AccessDeniedAuthorize]
    public class PurchaseItemController : Controller
    {
        public ActionResult Index(int PrId, bool EmptyLayout = false)
        {
            var service = new PurchaseRequestItemService.PurchaseRequestItemServiceClient();
            var service2 = new PurchaseRequestService.PurchaseRequestServiceClient();
            var RecordStatus = service2.Select(new PurchaseRequest { PrId = PrId }, null).FirstOrDefault().RecordStatus;
            var model = service.Select(new PurchaseRequestItem { PrId = PrId, RecordStatus = "!D" }, new[] { "MasResourceItem.MasUnit" });
            ViewBag.PrId = PrId;
            ViewBag.bEditable = RecordStatus == KPIS.GERP.Models.SYS.RecordStatus.WaitStatus || RecordStatus == KPIS.GERP.Models.SYS.RecordStatus.ReturnedStatus;
            ViewBag.EmptyLayout = EmptyLayout;
            return View(model);
        }

        public ActionResult Details(int Id)
        {
            var service = new PurchaseRequestItemService.PurchaseRequestItemServiceClient();
            var model = service.Select(new Models.ICM.PurchaseRequestItem { PrItemId = Id }, new[] {
                "PurchaseRequest.MasPurchaseType"
                , "PurchaseRequest.YearType"
                //, "ProjectOperationExpense.ProjectOperation"
                //, "ProjectOperationExpense.ProjectExpense.MasBudgetExpense"
                , "MasResourceItem.MasResourceSubGroup.MasResourceGroup.MasResourceType"
                , "MasResourceItem.MasUnit" }).FirstOrDefault();

            ViewBag.PcTypeId = model.PurchaseRequest.PcTypeId;
            ViewBag.PcTypeName = model.PurchaseRequest.MasPurchaseType.PcTypeName;
            ViewBag.YearTypeName = model.PurchaseRequest.YearType.YearTypeName;
            ViewBag.BgYear = model.PurchaseRequest.BgYear;
            ViewBag.bEditable = model.PurchaseRequest.RecordStatus == KPIS.GERP.Models.SYS.RecordStatus.WaitStatus || model.PurchaseRequest.RecordStatus == KPIS.GERP.Models.SYS.RecordStatus.ReturnedStatus;
            
            return View(model);
        }

        //public ActionResult Create(int PrId)
        //{
        //    var service = new PurchaseRequestService.PurchaseRequestServiceClient();
        //    var modelMain = service.Select(new Models.ICM.PurchaseRequest { PrId = PrId }, new[] { "YearType", "MasPurchaseType" }).FirstOrDefault();
        //    var model = new PurchaseRequestItem();
        //    model.PrId = PrId;

        //    ViewBag.PcTypeId = modelMain.PcTypeId;
        //    ViewBag.PcTypeName = modelMain.MasPurchaseType.PcTypeName;
        //    ViewBag.YearTypeName = modelMain.YearType.YearTypeName;
        //    ViewBag.BgYear = modelMain.BgYear;

        //    return View(model);
        //}

        //[HttpPost, ValidateAntiForgeryToken]
        //public ActionResult Create(PurchaseRequestItem model)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        try
        //        {
        //            model.MasResourceItem = null;//model.ResItemId.Value;
        //            model. = null;//model.ProjOperExpId.Value;

        //            var service = new PrePurchaseRequestItemService.PrePurchaseRequestItemServiceClient();
        //            service.Insert(model, Class.GimsMembership.CurrentEmpId);

        //            return RedirectToAction("Details", new { Id = model.PprItemId });
        //        }
        //        catch (Exception ex)
        //        {
        //            throw ex;
        //        }
        //    }

        //    return View(model);
        //}

        public ActionResult Edit(int Id)
        {
            var service = new PurchaseRequestItemService.PurchaseRequestItemServiceClient();
            var model = service.Select(new Models.ICM.PurchaseRequestItem { PrItemId = Id }, new[] {
                "PurchaseRequest.MasPurchaseType"
                , "PurchaseRequest.YearType"
                //, "ProjectOperationExpense.ProjectOperation"
                //, "ProjectOperationExpense.ProjectExpense.MasBudgetExpense"
                , "MasResourceItem.MasResourceSubGroup.MasResourceGroup.MasResourceType"
                , "MasResourceItem.MasUnit" }).FirstOrDefault();

            ViewBag.PcTypeId = model.PurchaseRequest.PcTypeId;
            ViewBag.PcTypeName = model.PurchaseRequest.MasPurchaseType.PcTypeName;
            ViewBag.YearTypeName = model.PurchaseRequest.YearType.YearTypeName;
            ViewBag.BgYear = model.PurchaseRequest.BgYear;

            return View(model);
        }

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult Edit(int Id, PurchaseRequestItem model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    model.MasResourceItem = null;//model.ResItemId.Value;
                    //model.ProjectOperationExpense = null;//model.ProjOperExpId.Value;

                    var service = new PurchaseRequestItemService.PurchaseRequestItemServiceClient();
                    service.Update(model, Class.GimsMembership.CurrentEmpId);

                    return RedirectToAction("Details", new { Id = model.PrItemId });
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return View(model);
        }


		public ActionResult Delete(int Id, int? PrId)
		{
			try
			{
                var service = new PurchaseRequestItemService.PurchaseRequestItemServiceClient();
                service.Delete(Id, Class.GimsMembership.CurrentEmpId);
				service.Close();
                return RedirectToAction("Index", new { PrId = PrId });
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		[HttpPost, ValidateAntiForgeryToken]
		public ActionResult DeleteAll(int[] deleteIds)
		{
			if (deleteIds == null)
				return Json(new { success = true });

			try
			{
                var service = new PurchaseRequestItemService.PurchaseRequestItemServiceClient();
				foreach (int delete in deleteIds)
				{
                    service.Delete(delete, Class.GimsMembership.CurrentEmpId);
				}

				ModelState.AddModelError(ErrorLocalization.Error, string.Format("ลบข้อมูลไปทั้งหมด {0} รายการ", deleteIds.Length.ToString("#,##0")));
				return Json(new { success = true });
			}
			catch (Exception ex)
			{
				ModelState.AddModelError(ErrorLocalization.Error, ErrorLocalization.ErrorWhileDeleteData);
				GerpLog.Error(ErrorLocalization.ErrorWhileDeleteData + ", " + ex.Message);
				return Json(new { success = false });
			}
		}

        //public ActionResult GetProjectOperationExpense(int Id)
        //{
        //    var service = new ProjectOperationService.ProjectOperationServiceClient();
        //    var result = service.SelectListExpense(new Models.BGM.ProjectOperationExpense() { ProjOperId = Id });
        //    return Json(result, JsonRequestBehavior.AllowGet);
        //}

        //public ActionResult GetProjectOperationExpenseInfo(int Id)
        //{
        //    var service = new ProjectOperationService.ProjectOperationServiceClient();
        //    var result = service.SelectExpense(new Models.BGM.ProjectOperationExpense() { ProjOperExpId = Id }, new[] { "ProjectExpense" }).FirstOrDefault();
        //    return Json(result, JsonRequestBehavior.AllowGet);
        //}

        //public ActionResult GetResourceTypeInfo(int ProjOperExpId)
        //{
        //    var service1 = new ProjectOperationService.ProjectOperationServiceClient();
        //    var ProjOperExpModel = service1.SelectExpense(new Models.BGM.ProjectOperationExpense() { ProjOperExpId = ProjOperExpId }
        //        , new[] { "ProjectExpense" }).FirstOrDefault();
        //    var ResItemId = ProjOperExpModel.ProjectExpense.ResItemId;
        //    var service2 = new MasResourceService.MasResourceServiceClient();

        //    if (ResItemId.HasValue)
        //    {
        //        var result = service1.SelectExpense(new Models.BGM.ProjectOperationExpense() { ProjOperExpId = ProjOperExpId }
        //            , new[] { "ProjectExpense.MasResourceItem.MasResourceSubGroup"
        //                , "ProjectExpense.MasResourceItem.MasResourceSubGroup.MasResourceGroup"
        //                , "ProjectExpense.MasResourceItem.MasResourceSubGroup.MasResourceGroup.MasResourceType"
        //                , "ProjectExpense.MasResourceItem.MasUnit" }).FirstOrDefault();
        //        return Json(result, JsonRequestBehavior.AllowGet);
        //    }
        //    else
        //    {
        //        var result = service2.SelectResourceType(new Models.ICM.MasResourceType() { ResTypeId = 1 }, null).FirstOrDefault();
        //        return Json(result, JsonRequestBehavior.AllowGet);
        //    }
        //}

        //public ActionResult GetResourceGroupList(int ResTypeId, int? ResGrpId = null)
        //{
        //    var service = new MasResourceService.MasResourceServiceClient();
        //    var result = service.SelectListResourceGroup(new Models.ICM.MasResourceGroup() { ResTypeId = ResTypeId });

        //    if (ResGrpId != null)
        //    {
        //        for (int i = 0; i < result.Length; i++)
        //        {
        //            if (ResGrpId.ToString() == result[i].Value)
        //            {
        //                result[i].Selected = true;
        //                break;
        //            }
        //        }
        //    }

        //    return Json(result, JsonRequestBehavior.AllowGet);
        //}

        //public ActionResult GetResourceSubGroupList(int ResGrpId)
        //{
        //    var service = new MasResourceService.MasResourceServiceClient();
        //    var result = service.SelectListResourceSubGroup(new Models.ICM.MasResourceSubGroup() { ResGrpId = ResGrpId });
        //    return Json(result, JsonRequestBehavior.AllowGet);
        //}

        //public ActionResult GetResourceItemList(int ResSubGrpId)
        //{
        //    var service = new MasResourceService.MasResourceServiceClient();
        //    var result = service.SelectListResourceItem(new Models.ICM.MasResourceItem() { ResSubGrpId = ResSubGrpId });

        //    return Json(result, JsonRequestBehavior.AllowGet);
        //}

        //public ActionResult GetResourceItemInfo(int Id)
        //{
        //    var service = new MasResourceService.MasResourceServiceClient();
        //    var result = service.SelectResourceItem(new Models.ICM.MasResourceItem() { ResItemId = Id }, new[] { "MasUnit" }).FirstOrDefault();

        //    return Json(result, JsonRequestBehavior.AllowGet);
        //}
    }
}
